import { getAction, deleteAction, putAction, postAction, httpAction } from '@/api/manage'
import { APP_CODE } from '@/store/mutation-types'
import Vue from 'vue'

import * as Depart from '@/views/system/depart/service.js'
import * as Dict from '@/views/sysConfig/dict/service.js'
import * as CheckRule from '@/views/sysConfig/checkRule/service.js'
import * as User from '@/views/system/user/service.js'
import { serviceApi } from '@/api/common.js'
/**
 * 公共API接口
 * @type {
 *  fileUpload: 默认上传地址
 *  importExcelUrl: 导入地址
 *  exportXlsUrl: 导出地址
 * }
 */
const apiUrl = {
  // 文件上传
  fileUpload: window._CONFIG['domianURL'] + '/auth/common/upload',
  importExcelUrl: uri => window._CONFIG['domianURL'] + `${uri}`,
  exportXlsUrl: uri => window._CONFIG['domianURL'] + `${uri}`,
  domianUrl: uri => window._CONFIG['domianURL'] + `${uri}`,
  // 预览图片&下载文件
  staticDomainURL: '/auth/common/static',
  // pdf预览Iframe
  pdfDomainUrl: '/auth/common/pdf/iframe-preview',
  // 中转HTTP请求，解决跨域问题
  transitRestful: '/auth/common/transit-restful',
  uploadApiPath:'property/upload-path'
}

/**
 * 通过用户token获取系统权限
 * @returns
 */
const queryPermissionsByUser = () => {
  let params = { appCode: APP_CODE }
  return getAction(serviceApi.auth + `/permissions/user-permissions`, params)
}

/**
 * 重复校验
 * @param {*} params
 * @returns
 */
const duplicateCheck = params => getAction(serviceApi.auth + '/duplicate/check', params)

/**
 * 获取部门树形结构列表
 *
 * @param {*} params
 * @returns
 *
 */
const selDepartTree = params => Depart.queryDepartTreeList(params)
/**
 * 树形结构加载异步
 * @param {*} params
 * @returns
 */
const queryDepartTreeSync = params => Depart.queryDepartTreeSync(params)

/**
 * queryDepartTreeList 兼容在线表单设计
 *
 * @param {*} params
 * @returns
 *
 */
const queryDepartTreeList = params => Depart.queryDepartTreeList(params)

/**
 * 通过部门ID获取用户列表
 *
 * @param {*} params
 * @returns
 */
const getUsersByDepId = (departId, params) => Depart.queryUserByDepId(departId, params)
/**
 * _queryUserByDepId 兼容在线表单设计
 * @param {*} params
 * @returns
 */
const queryUserByDepId = (departId, params) => Depart.queryUserByDepId(departId, params)

/**
 * 字典标签专用（通过code获取字典数组）
 * @param {*} params
 * @returns
 */
const getDictItemsByCode = params => Dict.getDictItemsByCode(params)

/**
 * 获取所有的字典Item列表
 * @param {*} params
 * @returns
 */
const queryDictItems = params => Dict.queryDictItems(params)

/**
 * 从缓存中获取字典配置
 * @param {*} params
 * @returns
 */
const getDictItemsFromCache = params => Dict.getDictItemsFromCache(params)
/**
 * 重复校验
 * @param {*} params
 * @returns
 */
const checkRuleByCode = (ruleCode, value, params) => CheckRule.checkRuleByCode(ruleCode, value, params)

/**
 * 获取用户列表
 * @param {*} params
 * @returns
 */
const getUserList = params => User.getUserList(params)

/**
 * 转换
 * @param {*} url
 * @returns
 */
const getTransitURL = url => apiUrl.transitRestful + `?url=${encodeURIComponent(url)}`

/**
 * 中转HTTP请求
 */
const transitRestful = {
  get: (url, parameter) => getAction(getTransitURL(url), parameter),
  post: (url, parameter) => postAction(getTransitURL(url), parameter),
  put: (url, parameter) => putAction(getTransitURL(url), parameter),
  http: (url, parameter) => httpAction(getTransitURL(url), parameter)
}

export {
  selDepartTree,
  queryDepartTreeList,
  getUsersByDepId,
  queryUserByDepId,
  queryPermissionsByUser,
  duplicateCheck,
  getDictItemsByCode,
  getDictItemsFromCache,
  checkRuleByCode,
  queryDictItems,
  getUserList,
  apiUrl,
  transitRestful,
  queryDepartTreeSync
}
